﻿// See https://aka.ms/new-console-template for more information

using EdgeTTS.Net;
using EdgeTTS.Net.Models;

EdgeTts.Await = true;
var option = new PlayOption
{
    Rate = 1,
    Text = ""
};
string msg;
Console.WriteLine("请输入文本内容.");
var ret = Console.ReadLine();
if (string.IsNullOrEmpty(ret))
{
    Console.WriteLine("文本内容为空，使用默认文本内容.");
    msg = "Hello EdgeTTS.Net";
}
else
{
    msg = ret;
}
option.Text = msg;
// 获取xiaoxiao语音包
var voice = EdgeTts.GetVoice()[55];
// 文字转语音，并且设置语速
var cts = new CancellationTokenSource();
cts.CancelAfter(2000);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "开始输出"); 
// EdgeTts.PlayText(option, voice);
await EdgeTts.PlayTextAsync(option, voice, cts.Token);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "结束输出");
Console.ReadLine();
return;


// 保存音频
static void SaveAudio()
{
    var option = new PlayOption
    {
        Rate = 0,
        Text = "Hello EdgeTTs",
        SavePath = "C:\\audio"
    };
    // 获取xiaoxiao语音包
    var voice = EdgeTts.GetVoice()
        .FirstOrDefault(i => i.Name == "Microsoft Server Speech Text to Speech Voice (zh-CN, XiaoxiaoNeural)");
    EdgeTts.SaveAudio(option, voice);
}

// 文本转语音
static void TextToAudio()
{
    var option = new PlayOption
    {
        Rate = 0,
        Text = "Hello EdgeTTs"
    };
    var voice = EdgeTts.GetVoice().First();
    EdgeTts.PlayText(option, voice);
}

// 自定义接口使用
static void MyFunc(string msg, Voice voice)
{
    var option = new PlayOption
    {
        Rate = 0,
        Text = msg
    };
    EdgeTts.Invoke(option, voice, libaray =>
    {
        // 写入自己的操作
        // ...
    });
}

// 获取一个PlayerAudio对象
static void getPlayer(string msg, Voice voice)
{
    var option = new PlayOption
    {
        Rate = 0,
        Text = msg
    };
    var player = EdgeTts.GetPlayer(option, voice);

    Console.WriteLine("开始播放");
    player.PlayAsync();
    Thread.Sleep(3000);


    Console.WriteLine("暂停播放");
    player.Pause();
    Thread.Sleep(3000);

    Console.WriteLine("继续播放");
    player.PlayAsync();
    Thread.Sleep(5000);

    player.Stop();
    Console.WriteLine("结束播放");
}